package com.woniu.cms.finance.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.cms.finance.core.entity.FinanceGathering;
import com.woniu.cms.finance.core.mapper.FinanceGatheringMapper;
import com.woniu.cms.finance.core.service.IFinanceGatheringService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cms.finance.core.vo.GetGathering;
import com.woniu.cms.finance.core.vo.GetGatheringAll;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收款表 服务实现类
 * </p>
 *
 * @author kiron
 * @since 2022-09-20
 */
@Service
public class FinanceGatheringServiceImpl extends ServiceImpl<FinanceGatheringMapper, FinanceGathering> implements IFinanceGatheringService {
    @Autowired
    private FinanceGatheringMapper financeGatheringMapper;

    @Override
    public Result getList() {
       List<GetGathering> getGatherings= financeGatheringMapper.getList();
        for (GetGathering getGathering : getGatherings) {
            BigDecimal subtract = getGathering.getInvoiceCount().subtract(getGathering.getGatheringCount());
            getGathering.setInvoiceNogathering(subtract);
        }
        return Result.ok();
    }

    @Override
    public Result addGathering(List<FinanceGathering> financeGatherings) {

        boolean b = this.saveBatch(financeGatherings);
        if(b){
            return Result.ok(BusinessCode.ADD_SUCCESS);
        }
        return Result.error(BusinessCode.ADD_FAIL);
    }

    @Override
    public Result getAll(Integer currentPage, Integer pageSize) {
        Page<GetGatheringAll> page = PageHelper.startPage(currentPage, pageSize).doSelectPage(() -> financeGatheringMapper.getAll());
        List<GetGatheringAll> result = page.getResult();
        long total = page.getTotal();
        Map map =new HashMap<>();
        map.put("records",result);
        map.put("total",total);

        return Result.common(map);
    }


}
